package com.cskaoyan.service.wx;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.wx.*;
//<<<<<<< HEAD
//import com.cskaoyan.mapper.wx.WxCommentMapper;
//=======
//import com.cskaoyan.bean.wxbean.OrderStatusBean;
//import com.cskaoyan.mapper.OrderMapper;
//import com.cskaoyan.mapper.UserMapper;
//>>>>>>> 1d88e251cb6efa85a619db2b6a25f4b69299a86e
import com.cskaoyan.mapper.*;
import com.cskaoyan.mapper.wx.WxCommentMapper;
import com.cskaoyan.mapper.wx.WxOrderGoodsMapper;
import com.cskaoyan.mapper.wx.WxOrderMapper;
import com.cskaoyan.utils.IdUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WxOrderServiceImpl implements WxOrderService {

    @Autowired
    WxOrderMapper wxOrderMapper;
    @Autowired
    WxOrderGoodsMapper wxOrderGoodsMapper;
    @Autowired
    WxCommentMapper wxCommentMapper;
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    CartMapper cartMapper;

    @Autowired
    CouponUserMapper couponUserMapper;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    CouponMapper couponMapper;

    @Override
    public OrderIdVO orderSubmit(WxOrderSubmitBO wxOrderSubmitBO) {
        Integer userId = IdUtils.getId();

        CartExample cartExample = new CartExample();
        cartExample.createCriteria().andUserIdEqualTo(userId).andCheckedEqualTo(true);
        List<Cart> carts = cartMapper.selectByExample(cartExample);
        Integer orderId = null;

        for (Cart cart : carts) {
            WxOrder wxOrder = new WxOrder();
            wxOrder.setUpdateTime(new Date());
            wxOrder.setAddTime(new Date());
            wxOrder.setDeleted(false);
            wxOrder.setMessage(wxOrderSubmitBO.getMessage());
            wxOrder.setUserId(userId);
            wxOrder.setOrderStatus((short) 201);
            Address address = addressMapper.selectByPrimaryKey(wxOrderSubmitBO.getAddressId());
            wxOrder.setAddress(address.getAddress());
            wxOrder.setConsignee(address.getName());
            wxOrder.setMobile(address.getMobile());
            wxOrder.setOrderSn(cart.getGoodsSn());
            wxOrder.setGoodsPrice(cart.getPrice());


            wxOrder.setFreightPrice(BigDecimal.valueOf(0));
            Coupon coupon = couponMapper.selectByPrimaryKey(wxOrderSubmitBO.getCouponId());
            if (coupon != null) {
                wxOrder.setCouponPrice(coupon.getDiscount());
                wxOrder.setActualPrice(cart.getPrice().subtract(coupon.getDiscount()));
            }else {
                wxOrder.setCouponPrice(BigDecimal.valueOf(0));
                wxOrder.setActualPrice(cart.getPrice());
            }
            wxOrder.setIntegralPrice(BigDecimal.valueOf(0));
            wxOrder.setOrderPrice(cart.getPrice());
            wxOrder.setGrouponPrice(cart.getPrice());
            wxOrderMapper.insertSelective(wxOrder);

            orderId = wxOrderMapper.selectLastInsertId();
            WxOrderGoods wxOrderGoods = new WxOrderGoods();
            wxOrderGoods.setOrderId(orderId);
            wxOrderGoods.setPicUrl(cart.getPicUrl());
            wxOrderGoods.setNumber(Short.valueOf(cart.getNumber().toString()));
            wxOrderGoods.setGoodsName(cart.getGoodsName());
            wxOrderGoods.setGoodsId(cart.getGoodsId());
            wxOrderGoods.setGoodsSn(cart.getGoodsSn());
            wxOrderGoods.setProductId(cart.getProductId());
            wxOrderGoods.setPrice(cart.getPrice());
            wxOrderGoods.setSpecifications(transfer(cart.getSpecifications()));
            wxOrderGoodsMapper.insertSelective(wxOrderGoods);

        }
        Integer couponId = wxOrderSubmitBO.getCouponId();
        CouponUserExample couponUserExample = new CouponUserExample();
        couponUserExample.createCriteria().andUserIdEqualTo(userId).andCouponIdEqualTo(couponId);
        List<CouponUser> couponUsers = couponUserMapper.selectByExample(couponUserExample);
        if (couponUsers.size() != 0) {
            CouponUser couponUser = couponUsers.get(0);
            couponUser.setStatus((short) 1);
            couponUserMapper.updateByExampleSelective(couponUser, couponUserExample);

        }


        OrderIdVO orderIdVO = new OrderIdVO();
        orderIdVO.setOrderId(orderId);
        return orderIdVO;
    }

    @SneakyThrows
    private String[] transfer(String specifications) {
        ObjectMapper objectMapper = new ObjectMapper();
        String[] strings = objectMapper.readValue(specifications, String[].class);
        return strings;
    }

    @Override
    public void orderPrepay(OrderIdVO orderIdVO) {
        WxOrder wxOrder = new WxOrder();
        wxOrder.setId(orderIdVO.getOrderId());
        short status = 201;
        wxOrder.setOrderStatus(status);
        wxOrder.setUpdateTime(new Date());
        wxOrderMapper.updateByPrimaryKeySelective(wxOrder);


    }

    @Override
    public WxOrderListBeanVO queryWxOrderList(Integer showType, Integer page, Integer size) {
        //分页
        PageHelper.startPage(page, size);
        //返回的ListBean
        WxOrderListBeanVO wxOrderListBeanVO = new WxOrderListBeanVO();


//////////////////

        WxOrderExample wxOrderExample = new WxOrderExample();
        WxOrderExample.Criteria criteria = wxOrderExample.createCriteria();
        //获得Id
        Integer id = IdUtils.getId();

        if (showType != 0) {
            int status = showType * 100 + 1;
            //提供条件
            criteria.andOrderStatusEqualTo((short) status);
            criteria.andUserIdEqualTo(id);
            criteria.andDeletedEqualTo(false);
        } else {
            criteria.andUserIdEqualTo(id);
            criteria.andDeletedEqualTo(false);
        }
        List<WxOrder> wxOrderList = wxOrderMapper.selectByExample(wxOrderExample);
        //封装data
        ArrayList<WxOrderListBean> wxOrderListBeanList = new ArrayList<>();
        for (WxOrder wxOrder : wxOrderList) {
            WxOrderListBean wxOrderListBean = new WxOrderListBean();
            wxOrderListBean.setGroupin(false);
            wxOrderListBean.setOrderSn(wxOrder.getOrderSn());
            wxOrderListBean.setActualPrice(Double.parseDouble(wxOrder.getActualPrice().toString()));
            wxOrderListBean.setId(wxOrder.getId());

            //封装orderStatusText
            Short orderStatus = wxOrder.getOrderStatus();
            String orderStatusText = getOrderStatusText(orderStatus);
            wxOrderListBean.setOrderStatusText(orderStatusText);

            //封装handleOption
            WxOrderHandleOption handleOption = getHandleOption(orderStatus);
            wxOrderListBean.setHandleOption(handleOption);

            //封装goodsList
            Integer orderId = wxOrder.getId();
            WxOrderGoodsExample wxOrderGoodsExample = new WxOrderGoodsExample();
            wxOrderGoodsExample.createCriteria().andOrderIdEqualTo(orderId).andDeletedEqualTo(false);
            List<WxOrderGoods> wxOrderGoods = wxOrderGoodsMapper.selectByExample(wxOrderGoodsExample);
            ArrayList<OrderWxListGoodsListBean> orderWxListGoodsListBeans = new ArrayList<>();
            for (WxOrderGoods wxOrderGood : wxOrderGoods) {
                OrderWxListGoodsListBean orderWxListGoodsListBean = new OrderWxListGoodsListBean();
                orderWxListGoodsListBean.setNumber((int) wxOrderGood.getNumber());
                orderWxListGoodsListBean.setPicUrl(wxOrderGood.getPicUrl());
                orderWxListGoodsListBean.setId(wxOrderGood.getId());
                orderWxListGoodsListBean.setGoodsName(wxOrderGood.getGoodsName());
                orderWxListGoodsListBeans.add(orderWxListGoodsListBean);
            }
            //给goodsList赋值
            wxOrderListBean.setGoodsList(orderWxListGoodsListBeans);
            //将一组data数据给list
            wxOrderListBeanList.add(wxOrderListBean);
        }

        //给count 和 totalPages赋值
        PageInfo pageInfo = new PageInfo(wxOrderList);
        long count = pageInfo.getTotal();
        int totalPageNum = pageInfo.getPageNum();
        wxOrderListBeanVO.setCount(count);
        wxOrderListBeanVO.setTotalPages(totalPageNum);
        //给data赋值
        wxOrderListBeanVO.setData(wxOrderListBeanList);

        return wxOrderListBeanVO;

    }


    @Override
    public WxOrderDetailVO queryWxOrderDetail(Integer orderId) {
        //共有三个  一个orderinfo  一个orderinfo内部的handleOption  一个ordergoods
        WxOrderDetailVO wxOrderDetailVO = new WxOrderDetailVO();
        //封装orderinfo
        WxOrder wxOrder = wxOrderMapper.selectByPrimaryKey(orderId);
        wxOrderDetailVO.setOrderInfo(wxOrder);
        //封装handleOption
        Short orderStatus = wxOrder.getOrderStatus();
        WxOrderHandleOption handleOption = getHandleOption(orderStatus);
        wxOrderDetailVO.getOrderInfo().setHandleOption(handleOption);
        //封装ordergoods
        WxOrderGoodsExample wxOrderGoodsExample = new WxOrderGoodsExample();
        wxOrderGoodsExample.createCriteria().andOrderIdEqualTo(orderId);
        List<WxOrderGoods> wxOrderGoods = wxOrderGoodsMapper.selectByExample(wxOrderGoodsExample);
        wxOrderDetailVO.setOrderGoods(wxOrderGoods);

        return wxOrderDetailVO;
    }

    @Override
    public void deleteWxOrder(Integer orderId) {
        WxOrder wxOrder = new WxOrder();
        wxOrder.setId(orderId);
        wxOrder.setUpdateTime(new Date());
        wxOrder.setDeleted(true);
        int i = wxOrderMapper.updateByPrimaryKeySelective(wxOrder);
    }

    @Override
    public void prepayWxOrder(Integer orderId) {
        WxOrder wxOrder = new WxOrder();
        wxOrder.setId(orderId);
        wxOrder.setUpdateTime(new Date());
        wxOrder.setOrderStatus((short) 201);
        int i = wxOrderMapper.updateByPrimaryKeySelective(wxOrder);

        OrderExample orderExample = new OrderExample();
        orderExample.createCriteria().andOrderStatusEqualTo((short) 201).andUserIdEqualTo(IdUtils.getId());
        List<Order> orders = orderMapper.selectByExample(orderExample);
        for (Order order : orders) {
            String orderSn = order.getOrderSn();
            CartExample cartExample = new CartExample();
            cartExample.createCriteria().andGoodsSnEqualTo(orderSn).andUserIdEqualTo(IdUtils.getId());
            cartMapper.deleteByExample(cartExample);
        }

    }

    @Override
    public void refundWxOrder(Integer orderId) {
        WxOrder wxOrder = new WxOrder();
        wxOrder.setId(orderId);
        wxOrder.setUpdateTime(new Date());
        wxOrder.setOrderStatus((short) 202);
        wxOrder.setDeleted(true);
        int i = wxOrderMapper.updateByPrimaryKeySelective(wxOrder);
    }

    @Override
    public void confirmWxOrder(Integer orderId) {
        WxOrder wxOrder = new WxOrder();
        wxOrder.setOrderStatus((short) 401);
        wxOrder.setUpdateTime(new Date());
        wxOrder.setId(orderId);
        int i = wxOrderMapper.updateByPrimaryKeySelective(wxOrder);
    }

    @Override
    public void updateWxOrderComment(WxOrderCommentBO wxOrderCommentBO) {
        Integer orderGoodsId = wxOrderCommentBO.getOrderGoodsId();
        WxOrderGoods wxOrderGoods = wxOrderGoodsMapper.selectByPrimaryKey(orderGoodsId);
        Integer goodsId = wxOrderGoods.getGoodsId();
        //******userId = 3
        Integer id = IdUtils.getId();
        wxOrderCommentBO.setUserId(id);

        WxComment wxComment = new WxComment();
        wxComment.setValueId(goodsId);
        wxComment.setUserId(wxOrderCommentBO.getUserId());
        wxComment.setContent(wxOrderCommentBO.getContent());
        wxComment.setStar(wxOrderCommentBO.getStar());
        wxComment.setHasPicture(wxOrderCommentBO.isHasPicture());
        wxComment.setPicUrls(wxOrderCommentBO.getPicUrls().toString());
        wxComment.setAddTime(new Date());
        wxComment.setUpdateTime(new Date());
        wxComment.setType((byte) 0);
        int i = wxCommentMapper.insertSelective(wxComment);

        //通过ordergoodsId 得到  orderId
        Integer orderId = wxOrderGoods.getOrderId();

        WxOrder wxOrder = new WxOrder();
        wxOrder.setUpdateTime(new Date());
        wxOrder.setId(orderId);
        wxOrder.setOrderStatus((short) 103);

        wxOrderMapper.updateByPrimaryKeySelective(wxOrder);

    }

    @Override
    public WxOrderGoods queryOrderGoods(Integer orderId, Integer goodsId) {
        WxOrderGoodsExample wxOrderGoodsExample = new WxOrderGoodsExample();
        wxOrderGoodsExample.createCriteria().andOrderIdEqualTo(orderId).andGoodsIdEqualTo(goodsId);
        List<WxOrderGoods> wxOrderGoods = wxOrderGoodsMapper.selectByExample(wxOrderGoodsExample);
        return wxOrderGoods.get(0);
    }

    private WxOrderHandleOption getHandleOption(Short orderStatus) {
        WxOrderHandleOption wxOrderHandleOption = new WxOrderHandleOption();
        //未付款  可以 取消 付款
        if (101 == orderStatus) {
            wxOrderHandleOption.setCancel(true);
            wxOrderHandleOption.setPay(true);
        }
        //未发货  可以 退款
        if (201 == orderStatus) {
            wxOrderHandleOption.setRefund(true);
        }
        //待收货  可以 确认收货  退款
        if (301 == orderStatus) {
            wxOrderHandleOption.setConfirm(true);
            wxOrderHandleOption.setRefund(true);
        }
        //已收货  可以 评论  再买一个  删除订单
        if (401 == orderStatus) {
            wxOrderHandleOption.setRebuy(true);
            wxOrderHandleOption.setComment(true);
            wxOrderHandleOption.setDelete(true);
        }
        return wxOrderHandleOption;
    }

    private String getOrderStatusText(int status) {
        String orderStatusText = "";
        if (status == 101) {
            orderStatusText = "未付款";
        }
        if (status == 102) {
            orderStatusText = "用户取消";
        }
        if (status == 103) {
            orderStatusText = "系统取消";
        }
        if (status == 201) {
            orderStatusText = "待发货";
        }
        if (status == 202) {
            orderStatusText = "申请退款";
        }
        if (status == 203) {
            orderStatusText = "已付款";
        }
        if (status == 301) {
            orderStatusText = "待收货";
        }
        if (status == 401) {
            orderStatusText = "待评价";
        }
        if (status == 402) {
            orderStatusText = "系统收货";
        }
        return orderStatusText;
    }
}
